package calorie.com.calorie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioGroup;

import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.net.ServiceGenerator;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import calorie.com.calorie.R;
import calorie.com.calorie.activity.NoticeListActivity;
import calorie.com.calorie.activity.WebViewActivity;
import calorie.com.calorie.constant.Constant;
import calorie.com.calorie.contact.HomeContact;
import calorie.com.calorie.response.BannerResponce;
import calorie.com.calorie.response.NoticeResponse;
import calorie.com.calorie.util.GlideImageLoader;
import calorie.com.calorie.view.ScrollTextView;
import retrofit2.Response;

/**
 * 首页
 */
public class HomeFragment extends MvpFragment<HomeContact.HomePresent> implements HomeContact.IHomeView {
    @BindView(R.id.banner)
    Banner banner;
    @BindView(R.id.list_notice_content)
    ScrollTextView list_notice_content;
    @BindView(R.id.rg)
    RadioGroup rg;
    private Fragment showFragment;
    private List<BannerResponce.DataBean> data;

    private List<String> title = new ArrayList<>();


    @Override
    public void initData(Bundle savedInstanceState) {
        rg.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_sport_data:
                    fragmentManager(R.id.fragment_container, new SportDataFragment(), "sport");
                    break;
                case R.id.rb_income_statistics:
                    fragmentManager(R.id.fragment_container, new IncomeStastisticsFragment(), "income");
                    break;
            }
        });
        rg.getChildAt(0).performClick();
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (data != null && data.size() > 0) {
                    String comment = data.get(position).getComment();
                    if (comment != null && !comment.equals("")) {
                        Intent intent = new Intent(context, WebViewActivity.class);
                        intent.putExtra("url", comment);
                        startActivity(intent);
                    }
                }
            }
        });
        mPresent.getBanner(context);
        mPresent.getNotice(context);

        list_notice_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, NoticeListActivity.class));

                banner.setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                        if (data != null && data.size() > 0) {
                            String comment = data.get(position).getComment();
                            if (comment != null && !comment.equals("")) {
                                Intent intent = new Intent(context, WebViewActivity.class);
                                intent.putExtra("url", comment);
                                startActivity(intent);
                            }
                        }

                    }
                });
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    private void fragmentManager(int resid, Fragment fragment, String tag) {
        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
        //开启一个事务
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //隐藏正在显示的Fragment对象
        if (showFragment != null) {
            fragmentTransaction.hide(showFragment);
        }

        Fragment mFragment = fragmentManager.findFragmentByTag(tag);
        if (mFragment != null) {
            fragmentTransaction.show(mFragment);
        } else {
            mFragment = fragment;
            fragmentTransaction.add(resid, mFragment, tag);
        }

        showFragment = mFragment;
        fragmentTransaction.commit();
    }

    @Override
    protected HomeContact.HomePresent createPresent() {
        return new HomeContact.HomePresent();
    }

    @Override
    public void getNoticeData(Response<NoticeResponse> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            List<NoticeResponse.DataBean> data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;

            // tvNoticeContent.setText(data.getTitle());
            title.clear();
            for (int i = 0; i < data.size(); i++) {
                title.add(data.get(i).getTitle());
            }
            list_notice_content.setList(title);
            list_notice_content.startScroll();


        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getBannerData(Response<BannerResponce> response) {
        if (response.body().getCode() == Constant.SUCCESS_CODE) {

            data = response.body().getData();

            if (ObjectUtils.isEmpty(data)) return;
            List<String> imgPath = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                imgPath.add(ServiceGenerator.BASE_URL + ":9023/image/" + data.get(i).getAddress());
            }
            banner.setImages(imgPath);
            banner.setImageLoader(new GlideImageLoader());
            banner.start();
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }


}
